package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.R;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by ZhaoJun on 2021/11/25 19:39
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmeaService;

    @PostMapping
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmealDto.status+''+#setmealDto.categoryId")
//    @CachePut(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmealDto.status+''+#setmealDto.categoryId")
    public R save(@RequestBody SetmealDto setmealDto) {
        log.info("添加套餐方法:{}", setmealDto);
        setmeaService.saveWithDish(setmealDto);

        return R.success("添加套餐成功");

    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        log.info("controller分页条件查询套餐:{}", page, pageSize, name);
        if (null == page || null == pageSize) {
            throw new CustomException("非法参数，查询条件含非法参数");
        }
        if (null == name) {
            name = "";
        }

        Page pageDto = setmeaService.pageAndCondition(page, pageSize, name);
        System.out.println("gitee");

        return R.success(pageDto);

    }

    @DeleteMapping
    @CacheEvict(value = "SETMEAL_CACHE",allEntries = true)
    public R del(Long[] ids){
        if(null==ids||ids.length==0){
            throw new CustomException("参数非法，ids");
        }
        Boolean flag = setmeaService.del(ids);

        if(flag){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @GetMapping("/{id}")
    @Cacheable(value = "SETMEAL_CACHE",key = "'SETMEAL'+id")
    public R findById(@PathVariable Long id){
        if(null == id){
           throw new CustomException("非法参数，id为null");
        }

        SetmealDto setmealDto = setmeaService.findById(id);

        return R.success(setmealDto);

    }

    @PutMapping
    @CacheEvict(value = "SETMEAL_CACHE",allEntries = true)
    public R updateWithDish(@RequestBody SetmealDto setmealDto){
        log.info("controller--updateById:{}",setmealDto);
        setmeaService.updateWithDish(setmealDto);

        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "SETMEAL_CACHE",allEntries = true)
    public R status(@PathVariable Integer status,Long[] ids){
        setmeaService.status(status,ids);
        return R.success("修改状态成功");

    }

    @GetMapping("/list")
    @Cacheable(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+''+#setmeal.categoryId")
    public R findList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId())
                .eq(setmeal.getStatus()!=0,Setmeal::getStatus,setmeal.getStatus())
                .orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmeaService.list(lqw);
        return R.success(setmeals);
    }









}
